﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tracy.BasicServices;
using Tracy.DataModel;
using Tracy.Handlers;
using Tracy.DataServices;

namespace Tracy
{
    /// <summary>
    /// General Access to Tracy Services & Resources
    /// </summary>
    public class TracyBus:IDisposable
    {
        #region Singleton
        /// <summary>
        /// Get the singleton instance
        /// </summary>
        public static TracyBus Instance
        {
            get { return SingletonHelper._instance; }
        }

        /// <summary>
        /// Nested helper class
        /// </summary>
        private class SingletonHelper
        {
            static SingletonHelper() { }
            internal static readonly TracyBus _instance = new TracyBus();
        }
        private TracyBus()
        {
            //Pre init codes come here
        }
        #endregion

        private ConfigurationManager _configurationManager = new ConfigurationManager();

        public ConfigurationManager ConfigurationManager
        {
            get { return _configurationManager; }
        }

        private TracyMessageDispatcher _messageDispatcher = new TracyMessageDispatcher();

        public TracyMessageDispatcher MessageDispatcher
        {
            get { return _messageDispatcher; }
        }

        private TracyPluginManager _pluginManager = new TracyPluginManager();

        public TracyPluginManager PluginManager
        {
            get { return _pluginManager; }
        }

        private TracyRegisteredObjectService _registeredObjectService = new TracyRegisteredObjectService();

        public TracyRegisteredObjectService RegisteredObjectService
        {
            get { return _registeredObjectService; }
        }

        private EntryProvider _entryProvider = new EntryProvider();

        public EntryProvider EntryProvider
        {
            get { return _entryProvider; }
        }

        private FilterProvider _filterProvider = new FilterProvider();

        public FilterProvider FilterProvider
        {
            get { return _filterProvider; }
        }

        private ResourceProvider _resourceProvider = new ResourceProvider();

        public ResourceProvider ResourceProvider
        {
            get { return _resourceProvider; }
        }

        private MediaFileProvider _mediaFileProvider = new MediaFileProvider();

        public MediaFileProvider MediaFileProvider
        {
            get { return _mediaFileProvider; }
        }

        private TaskScheduler _taskScheduler = new TaskScheduler();

        public TaskScheduler TaskScheduler
        {
            get { return _taskScheduler; }
        }

        private DownloadManager _downloadManager = new DownloadManager();

        public DownloadManager DownloadManager
        {
            get { return _downloadManager; }
        }


        public void Initialize()
        {
            //Init codes come here
            MessageDispatcher.SetHandlerCapacity(typeof(FeedScanHandler), 1); //must process one by one
            MessageDispatcher.SetHandlerCapacity(typeof(FileMonitorHandler), 1); //must process one by one
            MessageDispatcher.SetHandlerCapacity(typeof(CopyFileHandler), 1);//must process one by one

            MessageDispatcher.SetHandlerCapacity(typeof(GetXmlFromURLHandler), 5); //More threads for RSS reading
            RegisteredObjectService.RegisterInterface<ISourceParser>();
            RegisteredObjectService.RegisterInterface<IDownloadAdapter>();
            PluginManager.LoadPlugins();
            RegisteredObjectService.loadPlugins(PluginManager);
            TaskScheduler.LoadSettings(null);
            

            EntryProvider.LoadItems();
            FilterProvider.LoadItems();
            ResourceProvider.LoadItems();
            MediaFileProvider.LoadItems();
        }

        public void Dispose()
        {
            TaskScheduler.StopTimer();
            MessageDispatcher.Stop();
        }
    }
}
